<?php

namespace Admin\Controller;

use Common\Controller\AdminController;

class SystemController extends AdminController {

    public function index() {
        $account_user_id = $_SESSION['account_user_id'];
        $where['id'] = $account_user_id;
        $accountModel = new \Admin\Model\ShopAccountModel();
        $result = $accountModel->getSystemSet($where);
        $this->result = $result;
//        dump($result);die;
        $this->display("System/settings");
    }
    
    public function saveShopConfig(){
        $account_user_id = $_SESSION['account_user_id'];
        $where['id'] = $account_user_id;
//        $this->ajaxReturn($_POST);
        $data['shopname'] = $_POST['name'];
        $data['telphone'] = $_POST['tel'];
        $data['contacts'] = $_POST['contacts'];
        $yytime = explode('-', $_POST['yytime']);
        $data['opendt'] = $yytime[0];
        $data['closedt'] = $yytime[1];
        $data['address'] = $_POST['addres'];
        $data['returndt'] = $_POST['t_time'];
        $accountModel = new \Admin\Model\ShopAccountModel();
        $result = $accountModel->saveData($where, $data);
        if($result){
            $this->ajaxReturn(array('code'=>10000,'msg'=>'请求成功','data'=>array()));
        }else{
            $this->ajaxReturn(array('code'=>10000,'msg'=>'更新失败','data'=>array()));
        }
    }
    
    public function pwd(){
        $this->display();
    }
    
    public function savePasswd(){
        $account_user_id = $_SESSION['account_user_id'];
        $old_pwd = $_POST['old_pwd'];
        if(empty($old_pwd) && !isset($old_pwd)){
            $this->ajaxReturn(array('code'=>10001,'msg'=>'原密码不能为空','data'=>array()));
        }
        $new_pwd = $_POST['new_pwd'];
        $new_pwd1 = $_POST['new_pwd1'];
        if(empty($new_pwd) && !isset($new_pwd)){
            $this->ajaxReturn(array('code'=>10001,'msg'=>'新密码不能为空','data'=>array()));
        }
        if($new_pwd1 != $new_pwd){
            $this->ajaxReturn(array('code'=>10001,'msg'=>'两次输入密码不一致，请重输','data'=>array()));
        }
        $shopModel = new \Admin\Model\ShopAccountModel();
        $where['id'] = $account_user_id;
        $shop = $shopModel->getFind($where);
        
        if($shop['pwd'] != strtoupper(md5($old_pwd))){
            $this->ajaxReturn(array('code'=>10002,'msg'=>'原密码输入错误','data'=>array()));
        }
        $data['pwd'] = strtoupper(md5($new_pwd));
        $data['updatedt'] = date("Y-m-d H:i:s");
        $result = $shopModel->saveData($where, $data);
        if($result){
            $this->ajaxReturn(array('code'=>10000,'msg'=>'密码更新成功','data'=>array()));
        }else{
            $this->ajaxReturn(array('code'=>10003,'msg'=>'密码更新失败','data'=>array()));
        }
        
    }
    
    public function test(){
        echo strtoupper(md5("pwd"));
    }

}
